<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>打卡规则配置</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: '微软雅黑';
            background: rgb(255, 255, 255);
        }
        .main-content {
            width: 100%;
            padding: 24px;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        .action-bar {
            display: flex;
            gap: 12px;
            margin-bottom: 20px;
        }
        button {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 13px;
        }
        .btn-add {
            background: #1890ff;
            color: white;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            background: white;
            border: 1px solid #e8e8e8;
            margin-top: 12px;
        }
        th, td {
            padding: 12px 16px;
            text-align: left;
            border-bottom: 1px solid #e8e8e8;
            font-size: 13px;
        }
        th {
            background: #fafafa;
            color: rgba(0,0,0,0.85);
        }
        .action-btns button {
            padding: 4px 8px;
            margin-right: 8px;
        }
        .btn-edit {
            background: #faad14;
            color: #fff;
        }
        .btn-del {
            background: #ff4d4f;
            color: #fff;
        }
        .modal {
            display: none;
            position: fixed;
            z-index: 100;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }
        .modal-content {
            background-color: #fff;
            margin: 10% auto;
            padding: 20px;
            border-radius: 4px;
            width: 450px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.15);
        }
        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #e8e8e8;
        }
        .modal-title {
            font-size: 16px;
            color: rgba(0,0,0,0.85);
            font-weight: 500;
        }
        .close {
            font-size: 20px;
            color: #999;
            cursor: pointer;
        }
        .form-group {
            margin-bottom: 16px;
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: rgba(0,0,0,0.85);
        }
        .form-group input, .form-group select {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #d9d9d9;
            border-radius: 4px;
            font-size: 13px;
        }
        .modal-footer {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 24px;
            padding-top: 16px;
            border-top: 1px solid #e8e8e8;
        }
        .btn-cancel {
            background: #fff;
            color: rgba(0,0,0,0.65);
            border: 1px solid #d9d9d9;
        }
        .btn-save {
            background: #1890ff;
            color: white;
        }
  
            /* 添加开关样式 */
            .switch {
                position: relative;
                display: inline-block;
                width: 50px;
                height: 24px;
                vertical-align: middle;
                margin-right: 8px;
            }
            
            .switch input {
                opacity: 0;
                width: 0;
                height: 0;
            }
            
            .slider {
                position: absolute;
                cursor: pointer;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                background-color: #ccc;
                transition: .4s;
                border-radius: 24px;
            }
            
            .slider:before {
                position: absolute;
                content: "";
                height: 16px;
                width: 16px;
                left: 4px;
                bottom: 4px;
                background-color: white;
                transition: .4s;
                border-radius: 50%;
            }
            
            input:checked + .slider {
                background-color: #52c41a;
            }
            
            input:checked + .slider:before {
                transform: translateX(26px);
            }

    </style>
</head>
<body>
<div class="main-content">
    <h3 style="margin-bottom:16px;color:rgba(0,0,0,0.85)">打卡规则配置</h3>

    <div class="action-bar">
        <button class="btn-add" onclick="showAddModal()">新增规则</button>
    </div>

    <table>
        <thead>
        <tr>
            <th>规则名称</th>
            <th>开始时间</th>
            <th>结束时间</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
</div>

<!-- 新增/编辑规则模态框 -->
<div id="ruleModal" class="modal">
    <div class="modal-content">
        <div class="modal-header">
            <h4 class="modal-title" id="modalTitle">新增打卡规则</h4>
            <span class="close" onclick="hideModal()">&times;</span>
        </div>
        <!-- 修改后的表单部分 -->
        <form id="ruleForm">
            <input type="hidden" id="ruleId" name="id">
            <div class="form-group">
                <label for="ruleName">规则名称</label>
                <input type="text" id="ruleName" name="name" required>
            </div>
            <div class="form-group">
                <label for="startTime">开始时间</label>
                <input type="time" id="startTime" name="startTime" required>
            </div>
            <div class="form-group">
                <label for="endTime">结束时间</label>
                <input type="time" id="endTime" name="endTime" required>
            </div>
            <div class="form-group">
                <label>
                    <input type="checkbox" id="isActive" name="active" checked> 立即生效
                </label>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn-cancel" onclick="hideModal()">取消</button>
                <button type="submit" class="btn-save">保存</button>
            </div>
        </form>
    </div>
</div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xlsx@0.18.5/dist/xlsx.full.min.js"></script>
<script>
    // 页面加载时获取所有规则
    $(document).ready(function() {
        loadRules();
    });

    // 加载规则列表

    function loadRules() {
        $.ajax({
            url: '<%=request.getContextPath()%>/clockRuleController',
            type: 'POST',
            data: {op: 'findAll'},
            success: function(data) {
                renderRules(data);
            },
            error: function() {
                alert('加载规则失败');
            }
        });
    }

    // 渲染规则列表
    function renderRules(rules) {
        const tbody = $('table tbody');
        tbody.empty();
        let jsonData = JSON.parse(rules);
        let htmls = "";
        
        $.each(jsonData, function(index, rule) {
            htmls += '<tr>' +
                '<td>' + rule.name + '</td>' +
                '<td>' + rule.startTime + '</td>' +
                '<td>' + rule.endTime + '</td>' +
                '<td>' +
                '<label class="switch">' +
                '<input type="checkbox" ' + (rule.active ? 'checked' : '') + ' onchange="toggleRuleStatus(' + rule.id + ', this.checked)">' +
                '<span class="slider"></span>' +
                '</label>' +
                (rule.active ? '生效中' : '已停用') +
                '</td>' +
                '<td class="action-btns">' +
                '<button class="btn-edit" onclick="editRule(' + rule.id + ')">编辑</button>' +
                '<button class="btn-del" onclick="deleteRule(' + rule.id + ')">删除</button>' +
                '</td>' +
                '</tr>';
        });
        tbody.append(htmls);
    }

    // 显示新增规则模态框
    function showAddModal() {
        $('#modalTitle').text('新增打卡规则');
        $('#ruleForm')[0].reset();
        $('#ruleId').val('');
        $('#ruleModal').show();
    }

    // 显示编辑规则模态框
    function editRule(id) {
        $.ajax({
            url: '<%=request.getContextPath()%>/clockRuleController',
            type: 'POST',
            data: {op: 'findById', id: id},
            success: function(data) {
                const rule = JSON.parse(data); // 解析返回的JSON数据
                $('#modalTitle').text('编辑打卡规则');
                $('#ruleId').val(rule.id);
                $('#ruleName').val(rule.name);
                $('#startTime').val(rule.startTime.substring(0,5)); // 只取时分部分
                $('#endTime').val(rule.endTime.substring(0,5)); // 只取时分部分
                $('#isActive').prop('checked', rule.active);
                $('#ruleModal').show();
            },
            error: function() {
                alert('获取规则详情失败');
            }
        });
    }

    // 删除规则
    function deleteRule(id) {
        if(confirm('确定要删除这条规则吗？')) {
            $.ajax({
                url: '<%=request.getContextPath()%>/clockRuleController',
                type: 'POST',
                data: {op: 'delete', id: id},
                success: function(data) {
                    alert(data);
                    loadRules();
                },
                error: function() {
                    alert('删除规则失败');
                }
            });
        }
    }

    // 隐藏模态框
    function hideModal() {
        $('#ruleModal').hide();
    }

    // 表单提交处理
    $('#ruleForm').submit(function(e) {
        e.preventDefault();
        
        const rule = {
            id: $('#ruleId').val(),
            name: $('#ruleName').val(),
            startTime: $('#startTime').val(),
            endTime: $('#endTime').val(),
            active: $('#isActive').is(':checked')
        };
        
        const op = rule.id ? 'update' : 'save';
        
        $.ajax({
            url: '<%=request.getContextPath()%>/clockRuleController',
            type: 'POST',
            data: {
                op: op,
                rule: JSON.stringify(rule)
            },
            success: function(data) {
                alert(data);
                hideModal();
                loadRules();
            },
            error: function() {
                alert('操作失败');
            }
        });
    });

    // 切换规则状态
    function toggleRuleStatus(id, isActive) {
        $.ajax({
            url: '<%=request.getContextPath()%>/clockRuleController',
            type: 'POST',
            data: {
                op: 'toggleStatus',
                id: id,
                active: isActive
            },
            success: function(data) {
                alert(data);
                loadRules();
            },
            error: function() {
                alert('状态更新失败');
            }
        });
    }
</script>
</body>
</html>
